/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.table.person;

import java.awt.Graphics;

import javax.swing.event.ChangeEvent;

import twaver.TDataBox;
import twaver.table.PageListener;
import twaver.table.TElementTable;
import twaver.table.TTableColumn;

public class PersonTable extends TElementTable {
	
	private boolean lazyMode = false;
	private boolean invalidateRows = false;
	private boolean invalidateColumns = false;
	
	public PersonTable(TDataBox box) {
		super(box);
		
		this.getTableModel().addPageListener(new PageListener(){
			public void pageChanged() {	
				invalidateColumns();
				
		        int count = getColumnCount();
		        for (int columnIndex = 0; columnIndex < count; columnIndex++) {
		            TTableColumn column = (TTableColumn)getColumnModel().getColumn(columnIndex);
		            if(column.isRowPackParticipable()){
		            	invalidateRows();
		            	break;
		            }
		        }
			}
		});
	}
	
	private void invalidateRows(){
		if(lazyMode){
			invalidateRows = true;	
		}else{
			this.packAllRows();
		}
	}
	
	private void invalidateColumns(){
		if(lazyMode){
			invalidateColumns = true;
		}else{
			this.packAllColumns(true);	
		}
	}
	
	public void columnMarginChanged(ChangeEvent e) {
		super.columnMarginChanged(e);
		TTableColumn column = (TTableColumn)this.getResizingColumn();
		if(column != null && column.isRowPackParticipable()){
			invalidateRows();
		}
	}
	
    public void paintComponent(Graphics g) {
    	if(invalidateColumns){
			packAllColumns(true);
    		invalidateColumns = false;
    	}
        if(invalidateRows){
        	this.packAllRows();
        	invalidateRows = false;
        }
        super.paintComponent(g);
    }

}